/* util */
import { getRootWindowInitData } from '@src/util/window'
/* service */ 
import { isSystemAdmin } from '@service/RoleService.ts'
/* vue */
import { computed, ComputedRef, ref } from 'vue'
/* util */
import Log from '@src/util/log'
import Role from '@model/entity/Role/Role'

const rootWindowInitData = getRootWindowInitData()

export function useRole() {
  // 是否为系统管理员
  const isSystemManager: ComputedRef<boolean> = computed(() => isSystemManagerFunc())
  const isAdmin: ComputedRef<boolean | undefined> = computed(() => rootWindowInitData?.isAdmin || rootWindowInitData?.isFwyAdmin || rootWindowInitData?.isKfyAdmin)
  
  return {
    isSystemManager,
    isAdmin,
  }
}

function isSystemManagerFunc() {
  try {
    const roles: Role[] = rootWindowInitData?.user?.roles || []
    return roles.some(role => isSystemAdmin(role))
  } catch (error) {
    Log.error(error, isSystemManagerFunc.name);
    return false
  }
}